var express = require('express');
const bookinfor = express.Router();
var bodyParser = require('body-parser');
const path = require("path");
const Database = require("better-sqlite3");
//获取数据库中管理员账号密码等信息
const mydb = new Database(path.resolve(__dirname, "../database/infor.db"), { verbose: console.log });

// 创建 application/x-www-form-urlencoded 编码解析
bookinfor.use(bodyParser.urlencoded({ extended: false }))
bookinfor.use(express.json());
bookinfor.use(bodyParser.json());

bookinfor.post('/booksearch', function (req, res) {
    // 输出 JSON 格式
    var response = {
        "container": req.body.search_container,
        "methods": req.body.searchmethods
    };
    // console.log(response.container);
    // console.log(response.methods);
    // 判断是哪种检索方式
    if (response.methods == "ISBN") {
        //指定数据库文件路径
        const get_user_by_id = mydb.prepare('SELECT bookname,booktype,author,translator,publisher,price,bookcase,inventory,ISBN,introduce FROM book WHERE ISBN= ? and logoff=?');//一个查询语句对象
        var a = get_user_by_id.all(response.container, "no");//执行查询
        // console.log(a);
        if (a) {
            // 查询成功,
            console.log('查询成功');
            // 把查询到的数据写入json文件中
            const { writeFile } = require('fs');

            var t = { "code": 0, "msg": "", "count": 100, "data": a };
            const jsonpath = path.resolve(__dirname, "../public/js/bookdata.json");
            const config = t;
            writeFile(jsonpath, JSON.stringify(config, null, 2), (error) => {
                if (error) {
                    console.log('An error has occurred ', error);
                    return;
                }
                console.log('Data written successfully to disk');
            });
            res.sendFile(path.resolve(__dirname, "../public/html/book_search_result.html"));
        }
        else {
            console.log('没有这本书');
            // 查询失败,则返回查询界面
            res.sendFile(path.resolve(__dirname, "../public/html/search_error.html"));
        }
    }
    else if (response.methods == "书名") {
        //指定数据库文件路径
        const get_user_by_id = mydb.prepare('SELECT bookname,booktype,author,translator,publisher,price,bookcase,inventory,ISBN,introduce FROM book WHERE bookname like ? and logoff=?');//一个查询语句对象
        var a = get_user_by_id.all("%"+response.container+"%", "no");//执行查询
        // console.log(a);
        if (a) {
            // 查询成功,
            console.log('查询成功');
            // 把查询到的数据写入json文件中
            const { writeFile } = require('fs');

            var t = { "code": 0, "msg": "", "count": 100, "data": a };
            const jsonpath = path.resolve(__dirname, "../public/js/bookdata.json");
            const config = t;
            writeFile(jsonpath, JSON.stringify(config, null, 2), (error) => {
                if (error) {
                    console.log('An error has occurred ', error);
                    return;
                }
                console.log('Data written successfully to disk');
            });
            res.sendFile(path.resolve(__dirname, "../public/html/book_search_result.html"));
        }
        else {
            console.log('没有这本书');
            // 查询失败,则返回查询界面
            res.sendFile(path.resolve(__dirname, "../public/html/search_error.html"));
        }
    }
    else if (response.methods == "作者") {
        //指定数据库文件路径
        const get_user_by_id = mydb.prepare('SELECT bookname,booktype,author,translator,publisher,price,bookcase,inventory,ISBN,introduce FROM book WHERE author like ? and logoff=?');//一个查询语句对象
        var a = get_user_by_id.all("%"+response.container+"%","no");//执行查询
        // console.log(a);
        if (a) {
            // 查询成功,
            console.log('查询成功');
            // 把查询到的数据写入json文件中
            const { writeFile } = require('fs');

            var t = { "code": 0, "msg": "", "count": 100, "data": a };
            const jsonpath = path.resolve(__dirname, "../public/js/bookdata.json");
            const config = t;
            writeFile(jsonpath, JSON.stringify(config, null, 2), (error) => {
                if (error) {
                    console.log('An error has occurred ', error);
                    return;
                }
                console.log('Data written successfully to disk');
            });
            res.sendFile(path.resolve(__dirname, "../public/html/book_search_result.html"));
        }
        else {
            console.log('没有这本书');
            // 查询失败,则返回查询界面
            res.sendFile(path.resolve(__dirname, "../public/html/search_error.html"));
        }

    }
    else if (response.methods == "译者") {
        //指定数据库文件路径
        const get_user_by_id = mydb.prepare('SELECT bookname,booktype,author,translator,publisher,price,bookcase,inventory,ISBN,introduce FROM book WHERE translator like ? and logoff=?');//一个查询语句对象
        var a = get_user_by_id.all("%"+response.container+"%","no");//执行查询
        // console.log(a);
        if (a) {
            // 查询成功,
            console.log('查询成功');
            // 把查询到的数据写入json文件中
            const { writeFile } = require('fs');

            var t = { "code": 0, "msg": "", "count": 100, "data": a };
            const jsonpath = path.resolve(__dirname, "../public/js/bookdata.json");
            const config = t;
            writeFile(jsonpath, JSON.stringify(config, null, 2), (error) => {
                if (error) {
                    console.log('An error has occurred ', error);
                    return;
                }
                console.log('Data written successfully to disk');
            });
            res.sendFile(path.resolve(__dirname, "../public/html/book_search_result.html"));
        }
        else {
            console.log('没有这本书');
            // 查询失败,则返回查询界面
            res.sendFile(path.resolve(__dirname, "../public/html/search_error.html"));
        }

    }
    else if (response.methods == "出版社") {
        //指定数据库文件路径
        const get_user_by_id = mydb.prepare('SELECT bookname,booktype,author,translator,publisher,price,bookcase,inventory,ISBN,introduce FROM book WHERE publisher like ? and logoff=?');//一个查询语句对象
        var a = get_user_by_id.all("%"+response.container+"%","no");//执行查询
        // console.log(a);
        if (a) {
            // 查询成功,
            console.log('查询成功');
            // 把查询到的数据写入json文件中
            const { writeFile } = require('fs');

            var t = { "code": 0, "msg": "", "count": 100, "data": a };
            const jsonpath = path.resolve(__dirname, "../public/js/bookdata.json");
            const config = t;
            writeFile(jsonpath, JSON.stringify(config, null, 2), (error) => {
                if (error) {
                    console.log('An error has occurred ', error);
                    return;
                }
                console.log('Data written successfully to disk');
            });
            res.sendFile(path.resolve(__dirname, "../public/html/book_search_result.html"));
        }
        else {
            console.log('没有这本书');
            // 查询失败,则返回查询界面
            res.sendFile(path.resolve(__dirname, "../public/html/search_error.html"));
        }

    }
    else if (response.methods == "索书号") {
        //指定数据库文件路径
        const get_user_by_id = mydb.prepare('SELECT bookname,booktype,author,translator,publisher,price,bookcase,inventory,ISBN,introduce FROM book WHERE bookcase like ? and logoff=?');//一个查询语句对象
        var a = get_user_by_id.all("%"+response.container+"%","no");//执行查询
        // console.log(a);
        if (a) {
            // 查询成功,
            console.log('查询成功');
            // 把查询到的数据写入json文件中
            const { writeFile } = require('fs');

            var t = { "code": 0, "msg": "", "count": 100, "data": a };
            const jsonpath = path.resolve(__dirname, "../public/js/bookdata.json");
            const config = t;
            writeFile(jsonpath, JSON.stringify(config, null, 2), (error) => {
                if (error) {
                    console.log('An error has occurred ', error);
                    return;
                }
                console.log('Data written successfully to disk');
            });
            res.sendFile(path.resolve(__dirname, "../public/html/book_search_result.html"));
        }
        else {
            console.log('没有这本书');
            // 查询失败,则返回查询界面
            res.sendFile(path.resolve(__dirname, "../public/html/search_error.html"));
        }

    }
});

module.exports = bookinfor;